/*
 * @Author: your name
 * @Date: 2021-04-03 19:57:29
 * @LastEditTime: 2021-04-19 17:06:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\userMod\service.go
 */
package userMod

import (
	"chronic/common"
	"strconv"
)

func CreateUser(phone string, password string, role Role) (*User, error) {
	db := common.UseDb()
	passwordHashed := common.Hash(password)
	user := User{Password: passwordHashed, Phone: phone, Role: Role(role)}
	err := db.Save(&user).Error
	return &user, err
}

func FindUserByPhone(phone string) (User, error) {
	db := common.UseDb()
	var u User
	err := db.Where("phone = ?", phone).First(&u).Error
	return u, err
}
func FindAdminByPhone(phone string) (User, error) {
	db := common.UseDb()
	var u User
	err := db.Where("phone = ? AND role = ?", phone, 2).First(&u).Error
	return u, err
}
func FindUserById(id uint) (User, error) {
	db := common.UseDb()
	var u User
	err := db.Where("id = ?", id).First(&u).Error
	return u, err
}

func GetAllDoctors() (doctors []*User, err error) {
	db := common.UseDb()
	err = db.Where("role = ?", 1).Find(&doctors).Error
	return
}
func GetAllPatients() (patients []*User, err error) {
	db := common.UseDb()
	err = db.Where("role = ?", 0).Find(&patients).Error
	return
}
func GetAllAdmins() (admins []*User, err error) {
	db := common.UseDb()
	err = db.Where("role = ?", 2).Find(&admins).Error
	return
}
func GetAllUsers() (users []*User, err error) {
	db := common.UseDb()
	err = db.Find(&users).Error
	return
}

func updataOneInformation(id uint, input infoDto) (*User, error) {
	db := common.UseDb()
	user, err := FindUserById(id)
	switch input.Entry {
	case "name":
		user.Name = input.Info
	case "gender":
		user.Gender = input.Info
	case "phone":
		user.Phone = input.Info
	case "birth":
		user.Birthday = input.Info
	case "disease":
		user.Disease = input.Info
	}
	db.Save(&user)
	return &user, err
}

func adminUpdataUserInfo(input adminPutUserInfoDto) (*User, error) {
	db := common.UseDb()
	deal, _ := strconv.ParseUint(input.Deal, 10, 64)
	id, _ := strconv.ParseUint(input.ID, 10, 64)
	user, err := FindUserById(uint(id))
	if err != nil {
		return &user, err
	}
	user.Name = input.Name
	user.Gender = input.Gender
	user.Phone = input.Phone
	user.Birthday = input.Birthday
	switch user.Role {
	case 0:
		user.Disease = input.Disease
		user.Deal = uint(deal)
	case 1:
		user.DoctorDegree = input.Degree
		user.DoctorHospital = input.Hospital
		user.DoctorLevel = input.Level
	}
	db.Save(&user)
	return &user, err
}

func deleteUser(id uint) error {
	db := common.UseDb()
	user, err := FindUserById(id)
	if err != nil {
		return err
	}
	err2 := db.Delete(&user).Error
	return err2
}

func findUsersByCondition(condition string) ([]*User, error) {
	db := common.UseDb()
	var users []*User
	err := db.Where("name = ?", condition).Or("phone = ?", condition).Or("disease = ?", condition).Or("doctor_degree = ?", condition).Or("doctor_hospital = ?", condition).Find(&users).Error
	return users, err
}

func FindUsersByDeal(deal uint) ([]*User, error) {
	db := common.UseDb()
	var users []*User
	err := db.Where("deal = ?", deal).Find(&users).Error
	return users, err
}
